package net.sxnic.workflow.task.util;

import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sxnic.ugr.UgrConstant;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.task.TaskComment;

/**
 * 
 * Comments: TaskComment 工具类
 * <p>
 * Author：孙宇飞
 * <p>
 * Create Date：2009-10-22
 * <p>
 * 
 * 
 */
public class TaskCommentUtils {

	public static final String TASKCOMMENT_DATETIME_FORMAT = "yyyy-MM-dd";

	public static TaskComment formatTask(Task task) {
		return null;
	}

	/**
	 * 从一个task list中解构一个map。如
	 * <p>
	 * node1,taskComment:node1,fullName1...
	 * <p>
	 * node2,taskComment:node2,fullName2...
	 * 
	 * @param taskList
	 * @return
	 */
	public static Map<String, Set<TaskComment>> formatTasks(List<Task> taskList) {

		if (taskList == null || taskList.size() <= 0) {
			return new LinkedHashMap<String, Set<TaskComment>>();
		}

		Map<String, Set<TaskComment>> map = new LinkedHashMap<String, Set<TaskComment>>();

		for (Task task : taskList) {
			if (!map.containsKey(task.getName())) {
				map.put(task.getName(), findFromList(task.getName(), taskList));
			}
		}

		return map;
	}

	/**
	 * 从一个task list中，根据nodeName解构出一个TaskComment Set
	 * 
	 * @param nodeName
	 *            节点名
	 * @param taskList
	 *            任务list
	 * @return
	 */
	public static Set<TaskComment> findFromList(String nodeName,
			List<Task> taskList) {

		// TODO 处理排序
		Set<TaskComment> set = new LinkedHashSet<TaskComment>();
		TaskComment tc = null;
		SimpleDateFormat sf = new SimpleDateFormat(TASKCOMMENT_DATETIME_FORMAT);

		for (Task task : taskList) {
			if (task.getName().equals(nodeName)
					&& task.getStatus().equals(Task.TASK_STATUS_ENDED)) {
				tc = new TaskComment(nodeName, task.getAssignee(),
						UgrConstant.USER_MAP.get(task.getAssignee()),
						(task.getEndDate() == null) ? "" : sf.format(task
								.getEndDate()),
						(task.getEndDate() == null) ? ""
								: (task.getComments() == null ? "" : task
										.getComments()), "index",
						task.getDept());
				set.add(tc);
			}
		}

		return set;
	}

	public static String createTaskCommentsHtml(
			Map<String, Set<TaskComment>> map) {
		return "";
	}

	/**
	 * 根据taskComment set 生成意见列表
	 * 
	 * @param set
	 * @return
	 */
	public static String createTaskCommentsHtml(Set<TaskComment> set) {
		StringBuffer sb = new StringBuffer();
		for (TaskComment tc : set) {
			sb.append("&nbsp;&nbsp;" + tc.getComment() +"&nbsp;&nbsp;" + tc.getFullName() + "&nbsp;&nbsp;"
					+ tc.getTime() +  "<br/>");
		}

		return sb.toString();
	}
	
	/**
	 * 根据taskComment set 生成办理人及办理时间列表
	 * 
	 * @param set
	 * @return
	 */
	public static String createTaskNoCommentsHtml(Set<TaskComment> set) {
		StringBuffer sb = new StringBuffer();
		for (TaskComment tc : set) {
			sb.append("&nbsp;&nbsp;" + tc.getFullName() + "&nbsp;&nbsp;"
					+ tc.getTime() +  "<br/>");
		}

		return sb.toString();
	}

	/**
	 * 根据taskComment set 生成指定处室的意见列表。
	 * 
	 * @param set
	 * @param dept
	 *            处室名
	 * @return
	 */
	public static String createTaskCommentsHtmlByDept(Set<TaskComment> set,
			String dept) {
		StringBuffer sb = new StringBuffer();
		for (TaskComment tc : set) {
			if (dept.equals(tc.getDept())) {
				sb.append("&nbsp;&nbsp;" + tc.getComment()
						+"&nbsp;&nbsp;" + tc.getFullName() + "&nbsp;&nbsp;"
						+ tc.getTime() +  "<br/>");
			}
		}

		return sb.toString();
	}

	/**
	 * 根据taskComment set 生成指定处室的意见列表。
	 * 
	 * @param set
	 * @param dept
	 *            处室名
	 * @return
	 */
	public static String createTaskCommentsHtmlByUserName(Set<TaskComment> set,
			String userName) {
		StringBuffer sb = new StringBuffer();
		for (TaskComment tc : set) {
			if (userName.equals(tc.getUserName())) {
				sb.append("&nbsp;&nbsp;" + tc.getComment()
						+ "&nbsp;&nbsp;" + tc.getFullName() + "&nbsp;&nbsp;"
						+ tc.getTime() + "<br/>");
			}
		}

		return sb.toString();
	}
}
